package com.vast.ui.component.providers;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;

import com.vast.ui.component.models.ItemArray;
import com.vast.ui.component.models.ItemArrayListener;
import com.vast.ui.component.models.ItemPanel;

public class ItemContentProvider implements IStructuredContentProvider, ItemArrayListener {
	TableViewer viewer;
	
	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return ((ItemPanel) inputElement).getList().toArray();
	}

	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
		this.viewer = (TableViewer) viewer;
		
		if(oldInput != null) {
			((ItemPanel)oldInput).removeListener(this);
		}
		
		if(newInput != null) {
			((ItemPanel)newInput).addListener(this);
		}
	}

	public void add(ItemArray itemArray) {
		// TODO Auto-generated method stub
		if (viewer != null) {
			viewer.add(itemArray);
		}
	}

	public void remove(ItemArray itemArray) {
		// TODO Auto-generated method stub
		if (viewer != null) {
			viewer.remove(itemArray);
		}
	}
	
}
